import ScorePanel from "./ScorePanel";

export default class GameManager extends Laya.Script {

    constructor() { 
        super(); 
        /** @prop {name:scorePlanel, tips:"提示文本", type:Node, default:null}*/
        this.scorePlanel=null;
        /** @prop {name:txt_CountDown, tips:"提示文本", type:Node, default:null}*/
        this.txt_CountDown=null;
        /** @prop {name:gameOverPlane, tips:"提示文本", type:Node, default:null}*/
        this.gameOverPlane=null;
        /** @prop {name:pausePanel, tips:"提示文本", type:Node, default:null}*/
        this.pausePanel=null;
        /** @prop {name:btn_Pause, tips:"提示文本", type:Node, default:null}*/
        this.btn_Pause=null;


        this.scorePlanelScript=null;
        this.myScore=0;
        this.AIscore=0;
        this.timer=0;
        this.time=3;
        this.isStartGame=false;
        this.countDOwnTime=60;
        this.gameOver=false;
    ;
        // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0
    }
    onAwake(){

        Laya.SoundManager.playMusic("res1/ed.mp3",0)
        this.gameOverPlane.getChildByName("but_A").on(Laya.Event.CLICK,this,function(){
            Laya.Scene.open("Main.json")
        })
        this.gameOverPlane.getChildByName("btn").on(Laya.Event.CLICK,this,function(){
            Laya.Scene.open("Menu.json")
        })
        this.btn_Pause.on(Laya.Event.CLICK,this,function(){
            this.pausePanel.visible=true;
            Laya.timer.scale=0;
        })
        this.pausePanel.getChildByName("btn").on(Laya.Event.CLICK,this,function(){
            Laya.Scene.open("Menu.json")
            Laya.timer.scale=1;
        })
        this.pausePanel.getChildByName("but_B").on(Laya.Event.CLICK,this,function(){
            Laya.Scene.open("Main.json")
            Laya.timer.scale=1;
        })
        this.pausePanel.getChildByName("but_A").on(Laya.Event.CLICK,this,function(){
            this.pausePanel.visible=false;
            Laya.timer.scale=1;
        })



        this.scorePlanelScript=this.scorePlanel.getComponent(ScorePanel);
    }
    onUpdate(){
        if(this.isStartGame==false){
            Laya.timer.scale=1;
            this.timer+=Laya.timer.delta/1000;
            if(this.timer>=1){
                this.timer=0;
                this.time--;
                if(this.time<=0){
                    this.isStartGame=true;
                    this.txt_CountDown.visible=false;
                    this.StartGame()
                    return;
                }
                this.txt_CountDown.text=this.time
            }
            
        }else if(this.gameOver==false){
                this.timer+=Laya.timer.delta/1000;
            if(this.timer>=1){
                this.timer=0;
                this.countDOwnTime--;
                this.scorePlanelScript.Updatetime(this.countDOwnTime);
                if(this.countDOwnTime<=0){
                    this.gameOver=true;
                   this.GameOver()
                }
            }
            }
    } 
    GameOver(){
         this.gameOverPlane.visible=true;
                    Laya.stage.event("GameOver")
                
                    var txt_Result=this.gameOverPlane.getChildByName("txt_Result")
                    if(this.myScore>this.AIscore){
                        txt_Result.text="胜利"

                    }
                    else if(this.myScore<this.AIscore){
                        txt_Result.text="失败"

                    }
                    else{
                         txt_Result.text="平局"
                    }
    }
    StartGame(){
        this.btn_Pause.visible=true;
        Laya.SoundManager.playSound("res1/kai.WAV",1,
        new Laya.Handler(this,function () {
            Laya.stage.event("StarGame")
        }));

        

        }
    AddMyScore(){
        this.myScore++;
        this.scorePlanelScript.UpdateMyScore(this.myScore)
    }
    AddAIScore(){
        this.AIscore++;
        this.scorePlanelScript.UpdateAIScore(this.AIscore)
    }
    
    onEnable() {
    }

    onDisable() {
    }
}